package com.jxj.study.util;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

public class XmlUtil {

    public static Map<String,Object> parameter(Reader reader){
        Map<String,Object> map = new HashMap();
        try {
            XmlPullParserFactory xmlFactory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = xmlFactory.newPullParser();
            xmlPullParser.setInput(reader);
            int eventType = xmlPullParser.getEventType();
            while (eventType!=XmlPullParser.END_DOCUMENT){
                String name = xmlPullParser.getName();
                if (eventType==XmlPullParser.START_TAG && !"xml".equals(name)){
                    String text = xmlPullParser.nextText();
                    map.put(name,text);
                }
                eventType = xmlPullParser.next();
            }
        } catch (Exception e) {
            System.out.println("XML解析失败");
            e.printStackTrace();
        }
        return map;
    }
}
